import uuid

from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.

class User(AbstractUser):
    """
    用户model
    """
    id = models.CharField(max_length=128, primary_key=True)
    real_name = models.CharField(max_length=30, blank=True, null=True, verbose_name='真实姓名')
    sex = models.CharField(max_length=2, verbose_name='性别')
    age = models.CharField(max_length=3, verbose_name='年龄')
    id_card = models.CharField(max_length=18, blank=True, null=True, verbose_name='证件号码')
    phone_number = models.CharField(max_length=11, blank=True, null=True, verbose_name='联系电话')
    email = models.EmailField(null=True, unique=True, verbose_name='邮箱')
    is_auth = models.IntegerField(default=0, verbose_name='是否实名认证')
    summary = models.CharField(max_length=200, blank=True, null=True, verbose_name='个人简介')
    follow_count = models.IntegerField(default=0, verbose_name='关注数')
    fan_count = models.IntegerField(default=0, verbose_name='粉丝数')
    avatar = ProcessedImageField(upload_to='avatar',
                               default='avatar/default.png',
                               verbose_name='头像',
                               processors=[ResizeToFill(500, 500)],)

    class Meta(AbstractUser.Meta):
        verbose_name = '用户'
        verbose_name_plural = '用户'
        managed = True
        db_table = 'user'

    def __str__(self):
        return self.username

    def save(self, *args, **kwargs):
        if not self.id:
            self.id = uuid.uuid4()
        if len(self.avatar.name.split('/')) == 1:
            # 用户上传头像保存到以用户名命名的文件夹下
            self.avatar.name = self.username + '/avatar.' + self.avatar.name.split('.')[-1]

        super(User, self).save()

    def update(self):
        super(User, self).save()


class Certification(models.Model):
    """
    实名认证model
    """
    id = models.CharField(max_length=128, primary_key=True, verbose_name='Id')
    user = models.ForeignKey('User', db_column='user', on_delete=models.CASCADE, verbose_name='用户')
    real_name = models.CharField(max_length=10, null=True, verbose_name='真实姓名')
    id_card = models.CharField(max_length=18, blank=True, null=True, verbose_name='证件号码')
    certification_time = models.DateTimeField(auto_now_add=True, verbose_name='认证时间')
    id_card_positive = ProcessedImageField(blank=True, upload_to='certification',
                                                      default='certification/positive.png',
                                                      verbose_name='身份证正面',
                                                      processors=[ResizeToFill(600, 380)], )
    id_card_back = ProcessedImageField(blank=True, upload_to='certification',
                                                      default='certification/back.png',
                                                      verbose_name='身份证反面',
                                                      processors=[ResizeToFill(600, 380)], )


    class Meta:
        managed = True
        db_table = 'certification'
        verbose_name = '实名认证'
        verbose_name_plural = '实名认证'

    def __str__(self):
        return self.user.username

    def save(self, *args, **kwargs):
        if not self.id:
            self.id = uuid.uuid4()
        if self.id_card_positive:
            self.id_card_positive.name = str(self.user.username) + 'positive.jpg'
        if self.id_card_back:
            self.id_card_back.name = str(self.user.username) + 'back.jpg'
        super(Certification, self).save()


class LimitLike(models.Model):
    """
    点赞限制model
    """
    # 级联删除需设置on_delete为models.CASCADE,否则后台管理删除音乐或帖子时会报错
    id = models.CharField(max_length=128, primary_key=True, verbose_name='Id')
    user = models.ForeignKey('User', db_column='user', on_delete=models.CASCADE, verbose_name='用户')
    music = models.ForeignKey('music.MusicList', null=True, blank=True, db_column='music', on_delete=models.CASCADE,
                              verbose_name='音乐')
    post = models.ForeignKey('post.PostList', null=True, blank=True, db_column='post', on_delete=models.CASCADE,
                              verbose_name='主题')

    class Meta:
        db_table = 'limit_like'
        managed = True

    def save(self, *args, **kwargs):
        if not self.id:
            self.id = uuid.uuid4()
        super(LimitLike, self).save()


class UserFollow(models.Model):
    """
    用户关注model
    """
    id = models.CharField(max_length=128, primary_key=True, verbose_name='Id')
    follow = models.ForeignKey('User', db_column='follow', on_delete=models.CASCADE, related_name='follow_user', verbose_name='关注者')
    fan = models.ForeignKey('User', db_column='fan', on_delete=models.CASCADE, related_name='fan_user', verbose_name='被关注者')
    follow_date = models.DateTimeField(auto_now_add=True, verbose_name='关注时间')

    class Meta:
        db_table = 'user_follow'
        managed = True

    def save(self, *args, **kwargs):
        if not self.id:
            self.id = uuid.uuid4()
        super(UserFollow, self).save()